﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace DotNet.Common
{
	public struct ToolStripTeam
	{
		public ToolStripMenuItem StripMenu;
		public ToolStrip Strip;

		public ToolStripTeam(ToolStripMenuItem menu, ToolStrip strip)
		{
			StripMenu = menu;
			Strip = strip;
		}
	}

	public class ToolStripMenuList
	{
		List<ToolStripMenu> m_toolStripMenuList;

		[XmlArray("Items"), XmlArrayItem("Item")]
		public List<ToolStripMenu> Items
		{
			get { return m_toolStripMenuList; }
			set { }
		}

		public ToolStripMenuList()
		{
			m_toolStripMenuList = new List<ToolStripMenu>();
		}

		public void Load(params ToolStripTeam[] toolStripArray)
		{
			if (toolStripArray == null)
				return;

			int i;
			int arrayCount = toolStripArray.Length;
			if (m_toolStripMenuList.Count == arrayCount)
			{
				for (i = 0; i < arrayCount; i++)
				{
					m_toolStripMenuList[i].Load(toolStripArray[i]);
				}
			}
			else
			{
				m_toolStripMenuList.Clear();
				ToolStripMenu[] menuArray = new ToolStripMenu[arrayCount];
				for (i = 0; i < arrayCount; i++)
				{
					menuArray[i] = new ToolStripMenu(toolStripArray[i]);
				}
				m_toolStripMenuList.AddRange(menuArray);
			}
		}
	}

	public class ToolStripMenu
	{
		bool m_constructionFlag;
		ToolStripTeam m_toolStripTeam;

		[XmlElement("Name")]
		public string Name
		{
			get
			{
				return m_toolStripTeam.StripMenu.Name;
			}
			set
			{

			}
		}

		[XmlElement("Checked")]
		public bool Checked
		{
			get
			{
				return m_toolStripTeam.StripMenu.Checked;
			}
			set
			{
				m_toolStripTeam.StripMenu.Checked = m_toolStripTeam.Strip.Visible = value;
			}
		}

		[XmlElement("X")]
		public int X
		{
			get
			{
				return m_toolStripTeam.Strip.Location.X;
			}
			set
			{
				bool disvisible = !m_toolStripTeam.Strip.Visible;
				if (disvisible)
					m_toolStripTeam.Strip.Visible = true;

				Point p = m_toolStripTeam.Strip.Location;
				p.X = value;
				m_toolStripTeam.Strip.Location= p;

				if (disvisible)
					m_toolStripTeam.Strip.Visible = false;
			}
		}

		[XmlElement("Y")]
		public int Y
		{
			get
			{
				return m_toolStripTeam.Strip.Location.Y;
			}
			set
			{
				bool disvisible = !m_toolStripTeam.Strip.Visible;
				if (disvisible)
					m_toolStripTeam.Strip.Visible = true;

				Point p = m_toolStripTeam.Strip.Location;
				p.Y = value;
				m_toolStripTeam.Strip.Location = p;

				if (disvisible)
					m_toolStripTeam.Strip.Visible = false;
			}
		}

		public ToolStripMenu()
		{
			m_constructionFlag = true;
			m_toolStripTeam.StripMenu = new ToolStripMenuItem();
			m_toolStripTeam.Strip = new ToolStrip();
		}

		public ToolStripMenu(ToolStripTeam toolStripTeam)
		{
			m_constructionFlag = false;
			Load(toolStripTeam);
		}

		public void Load(ToolStripTeam toolStripTeam)
		{
			if (toolStripTeam.StripMenu == null || toolStripTeam.Strip == null)
				return;

			if (m_constructionFlag)
			{
				if (!toolStripTeam.Strip.Visible)
					toolStripTeam.Strip.Visible = true;

				toolStripTeam.Strip.Location = new Point(this.X, this.Y);
                toolStripTeam.StripMenu.Checked = toolStripTeam.Strip.Visible = this.Checked;
			}
            toolStripTeam.StripMenu.CheckOnClick = true;
			toolStripTeam.StripMenu.Click += new EventHandler(stripMenu_Click);

			m_toolStripTeam = toolStripTeam;
		}

		void stripMenu_Click(object sender, EventArgs e)
		{
			m_toolStripTeam.Strip.Visible = m_toolStripTeam.StripMenu.Checked;
		}
	}
}
